package com.dwxt.community.admin.controller;

import com.alipay.api.domain.UserVo;
import com.dwxt.common.annotation.ValidateFiled;
import com.dwxt.common.annotation.ValidateGroup;
import com.dwxt.common.base.BaseResult;
import com.dwxt.community.admin.service.UserServiceAdmin;
import com.dwxt.community.entity.User;
import com.dwxt.community.entity.vo.TrashIntegralRecoadVo;
import com.dwxt.community.entity.vo.UserVO;
import com.dwxt.community.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RequestMapping("/cms/user")
@RestController
public class UserControllerAdmin {

    @Autowired
    private UserServiceAdmin userServiceAdmin;

    @Autowired
    private UserService userService;

    @RequestMapping("/selectUserPage")
    public BaseResult selectUserPage(Integer pageNum, Integer pageSize, User user) {
        PageHelper.startPage(pageNum, pageSize);
        return userServiceAdmin.selectUserList(user);
    }


    @RequestMapping("/createUser")
    @ValidateGroup(fileds = {
            @ValidateFiled(index = 0, filedName = "phone", regStr = "^1[3|4|5|6|7|8|9][0-9]\\d{4,8}$"),
            @ValidateFiled(index = 0, filedName = "username", notNull = true),
            @ValidateFiled(index = 0, filedName = "identityCard", regStr = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)"),
            @ValidateFiled(index = 0, filedName = "communityId", notNull = true),
            @ValidateFiled(index = 0, filedName = "communityName", notNull = true),
            @ValidateFiled(index = 0, filedName = "houseNumber", notNull = true),
    })
    public BaseResult createUser(User user) {
        return userService.createUser(user);
        //return BaseResult.ok();
    }

    @RequestMapping("/selectByPhone")
    public BaseResult selectByPhone(String phone) {
        User user = userService.selectByPhone(phone);
        return BaseResult.ok(user);
    }

    @RequestMapping("/selectDemonstrationUserPage")
    public BaseResult selectDemonstrationUserPage(Integer pageNum, Integer pageSize, UserVO userVo) {
        PageInfo<User> pageInfo = userServiceAdmin.selectDemonstrationUserList(pageNum, pageSize, userVo);
        return BaseResult.ok(pageInfo);
    }


    @RequestMapping("/updateUser")
    public BaseResult updateUser(User user){
        userServiceAdmin.updateUser(user);
        return BaseResult.ok();
    }

    @RequestMapping("/export")
    public void recordExport(HttpServletResponse response, UserVO userVo) {
        userServiceAdmin.recordExport(response, userVo);
    }

}
